<?php

namespace app\api\home;

use app\api\model\example;
use app\api\model\ImGameCategory;
use app\api\model\ImGroup;
use app\api\model\ImGroupUser;
use app\api\model\RtcWheat;
use app\api\validate\GameCategoryValidate;
use app\api\validate\RtcPlayValidate;
use app\api\validate\RtcWheatValidate;
use app\service\audioVideo\RtcChannel;
use app\service\audioVideo\Base as audioVideoBase;
use app\api\validate\RtcAnchorValidate;
use app\api\model\RtcAnchor;
use app\api\model\RtcPlay;

class RtcController extends Base
{
    /**
     * 获取发起频道鉴权token
     */
    public function getRtcToken()
    {
        $params = $this->request->param();

        // 验证
        $validate = new RtcAnchorValidate;
        if (!$validate->scene('getToken')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $res = new audioVideoBase();
        $data = $res->getToken($this->uid, $params['channel_id']);
        return_ajax(1, '操作成功', $data);

    }

    /**
     * Notes 创建房间
     * User: tangfen
     * Date: 2023/12/20
     * Time: 14:02
     */
    public function getRtcUrl()
    {
        $params = $this->request->post();
        // 验证
        $validate = new RtcAnchorValidate;
        if (!$validate->scene('add')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $anchorId = RtcAnchor::where(['channel_id' => $params['channel_id'], 'uid' => $this->uid])->value('id');
        if (empty($anchorId)) {

            //创建im群
            $imGroup = new ImGroup();
            $imGroupId = $imGroup->addRtcGroup(strval($this->uid), $params['name'], $params['channel_id'], $params['wheat_duration'], $params['num'], $params['game_category_id'], $params['channel_announcement']);
            $params['im_group_id'] = $imGroupId;

            //创建房间和群主也加入房间
            $rtcAnchoor = new RtcAnchor();
            $params['uid'] = $this->uid;
            $res = $rtcAnchoor->insertRtcAnchor($params);

            return_ajax(1, '操作成功', $res);
        } else {
            return_ajax(0, '已存在该频道');
        }
    }

    /**
     * Notes 编辑房间
     * User: tangfen
     * Date: 2023/12/20
     * Time: 14:02
     */
    public function editChannel()
    {
        $params = $this->request->param();
        // 验证
        $validate = new RtcAnchorValidate;
        if (!$validate->scene('edit')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $rtcAnchoor = new RtcAnchor();
        $res = $rtcAnchoor->editRtcAnchor($params);

        if ($res) {
            return_ajax(1, '操作成功', $res);
        } else {
            return_ajax(0, '操作失败');
        }
    }

    /**
     * Notes 房间详情
     * User: tangfen
     * Date: 2023/12/20
     * Time: 14:02
     */
    public function channelDetails()
    {
        $params = $this->request->param();
        // 验证
        $validate = new RtcAnchorValidate;
        if (!$validate->scene('details')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $rtcAnchoor = new RtcAnchor();
        $res = $rtcAnchoor->channelDetails($params);

        if ($res) {
            return_ajax(1, '获取成功', $res);
        } else {
            return_ajax(0, '获取失败');
        }
    }

    /**
     * Notes:获取房间列表
     * User: tangfen
     * Date: 2024/1/3
     * Time: 13:06
     */
    public function getRtcList()
    {
        $params = $this->request->get();
        // 验证
        $validate = new RtcAnchorValidate;
        if (!$validate->scene('list')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $list = (new RtcAnchor)->getRtcList($params);
        $res = $list->items();  //列表

        $data['list'] = $res;
        $data['page'] = $params['page'];
        $data['count'] = $list->total();//总条数
        return_ajax(1, '操作成功', $data);

    }

    //加入房间频道
    public function addChannel()
    {
        $params = $this->request->post();
        // 验证
        $validate = new RtcPlayValidate;
        if (!$validate->scene('join')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $res = RtcPlay::where(['uid' => $this->uid, 'channel_id' => $params['channel_id']])->value('id');
        if (empty($res)) {

            //获取腾讯群id
            $rtcAnchoor = new RtcAnchor();
            $GroupId = $rtcAnchoor->getGroupId($params['channel_id']);

            //加入群成员
            $res = new ImGroup;
            $params['group_id'] = $GroupId;
            $params['uid'] = $this->uid;
//            $params['uid'] = 3;
            $data = $res->addGroupMember($params);

            if ($data == 'true') {
                return_ajax(1, '操作成功', $data);
            } else {
                return_ajax(0, '加入失败');
            }
        } else {
            return_ajax(0, '该用户已加入该房间');
        }

    }

    //删除房间
    public function deleteChannel()
    {
        $params = $this->request->post();

        // 验证
        $validate = new RtcAnchorValidate;
        if (!$validate->scene('del')->check($params)) {
            return_ajax(0, $validate->getError());
        }

        $rtcAnchor = RtcAnchor::where('id', $params['id'])->field('channel_id,im_group_id')->find();

        //解散腾讯im群
        $rtcAnchoor = new RtcAnchor();
        $result = $rtcAnchoor->deleteGroup($rtcAnchor['channel_id']);//解散腾讯im群

        //删除阿里云音视频频道
        $res = new RtcChannel();
        $data = $res->deleteChannel($rtcAnchor['channel_id']);

        if ($data == 'true' && $result == 'true') {
            RtcAnchor::where('id', $params['id'])->update(['status' => 1]);// 房间 软删除  更新状态
            ImGroup::where('id', $rtcAnchor['im_group_id'])->update(['status' => 2]);// im群表 1=正常，2=解散
            return_ajax(1, '操作成功', $data);
        } else {
            return_ajax(0, '操作失败', $data);
        }

    }


    //退出房间
    public function RemoveTerminals()
    {
        $params = $this->request->post();

        // 验证
        $validate = new RtcAnchorValidate;
        if (!$validate->scene('tichu')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $uid = strval($this->uid);
         $homeownerId = RtcAnchor::where('channel_id',$params['channel_id'])->value('uid');
        if($homeownerId == $uid){
            return_ajax(0, '房主不能退出房间，要退出先转让房主');
        }

        //从腾讯im群踢出用户
        //踢出腾讯im群
        $rtcAnchoor = new RtcAnchor();
        $data = $rtcAnchoor->RemoveGroup($params['channel_id'], $uid);//解散腾讯im群

//        //把阿里云音视频用户踢出频道  前端踢
//        $res = new RtcChannel();
//        $data = $res->RemoveTerminals($terminalIds, $params['channel_id']);

        if ($data == 'true') {
            // 房间用户 软删除  更新状态
            $res = RtcPlay::where(['uid' => $uid, 'channel_id' => $params['channel_id']])->update(['status' => 2]);
            //im群用户 软删
            $rtcAnchoor = new RtcAnchor();
            $GroupId = $rtcAnchoor->getGroupId($params['channel_id']);
            $result = ImGroupUser::where(['uid' => $uid, 'GroupId' => $GroupId])->update(['status' => 2]);
            if ($res !==false && $result!==false) {
                return_ajax(1, '操作成功', $data);
            } else {
                return_ajax(0, '操作失败', $data);
            }
        } else {
            return_ajax(0, '操作失败', $data);
        }
    }

    /**
     * Notes:获取房间分类别列表
     * User: tangfen
     * Date: 2023/12/28
     * Time: 12:11
     */
    public function gameCategoryList()
    {
        $params = $this->request->get();
        // 验证
        $validate = new GameCategoryValidate;
        if (!$validate->scene('list')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $list = (new ImGameCategory)->gameCategoryList($params);
        $res = $list->items();  //列表

        $data['list'] = $res;
        $data['page'] = $params['page'];
        $data['count'] = $list->total();//总条数
        return_ajax(1, '操作成功', $data);
    }

    /**
     * Notes:获取房间正式成员列表
     * User: tangfen
     * Date: 2023/12/28
     * Time: 17:24
     */
    public function getChannelUserList()
    {
        $params = $this->request->get();
        // 验证
        $validate = new RtcPlayValidate;
        if (!$validate->scene('list')->check($params)) {
            return_ajax(0, $validate->getError());
        }

        $list = (new RtcPlay)->rtcPlayFormalList($params);
        $res = $list->items();  //列表

        $data['list'] = $res;
        $data['page'] = $params['page'];
        $data['count'] = $list->total();//总条数
        return_ajax(1, '操作成功', $data);
    }

    /**
     * Notes:抢麦
     * User: tangfen
     * Date: 2023/12/28
     * Time: 19:07
     */
    public function userWheat()
    {
        $params = $this->request->post();
        // 验证
        $validate = new RtcWheatValidate;
        if (!$validate->scene('add')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
        if ($params['status'] == 1) {//抢麦
            $res = (new RtcWheat)->addWheat($params);
        } else {//下麦
            $res = (new RtcWheat)->delWheat($params);
        }
        if ($res) {
            return_ajax(1, '操作成功', $res);
        } else {
            return_ajax(0, '入库失败');
        }
    }

    /**
     * Notes:抢麦列表
     * User: tangfen
     * Date: 2023/12/28
     * Time: 19:32
     */
    public function WheatList()
    {
        $params = $this->request->get();
        // 验证
        $validate = new RtcWheatValidate;
        if (!$validate->scene('list')->check($params)) {
            return_ajax(0, $validate->getError());
        }

        $list = (new RtcWheat)->WheatList($params);
        $res = $list->items();  //列表

        $data['list'] = $res;
        $data['page'] = $params['page'];
        $data['count'] = $list->total();//总条数
        return_ajax(1, '操作成功', $data);
    }

    /**
     * Notes:房主禁麦
     * User: tangfen
     * Date: 2023/12/28
     * Time: 19:53
     */
    public function prohibitWheat()
    {
        $params = $this->request->post();
        // 验证
        $validate = new RtcAnchorValidate;
        if (!$validate->scene('prohibitWheat')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $res = (new RtcAnchor)->prohibitWheat($params);
        if ($res == 'true') {
            return_ajax(1, '操作成功', $res);
        } else {
            return_ajax(0, '禁麦失败');
        }
    }

    /**
     * Notes:房主控麦
     * User: tangfen
     * Date: 2023/12/28
     * Time: 19:53
     */
    public function controlWheat()
    {
        $params = $this->request->post();
        // 验证
        $validate = new RtcAnchorValidate;
        if (!$validate->scene('controlWheat')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $res = (new RtcAnchor)->controlWheat($params);
        if ($res == 'true') {
            return_ajax(1, '操作成功', $res);
        } else {
            return_ajax(0, '房主控麦');
        }
    }

    /**
     * Notes:判断用户是否为房主
     * User: tangfen
     * Date: 2023/12/28
     * Time: 20:18
     */
    public function isHomeowner()
    {
        $params = $this->request->post();
        // 验证
        $validate = new RtcAnchorValidate;
        if (!$validate->scene('isHomeowner')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
        $res = (new RtcAnchor)->isHomeowner($params);
        return_ajax(1, '操作成功', $res);
    }

    /**
     * Notes:判断抢麦用户是否可以发言(说话)
     * User: tangfen
     * Date: 2023/12/28
     * Time: 20:37
     */
    public function isCanTalk()
    {
        $params = $this->request->post();
        // 验证
        $validate = new RtcAnchorValidate;
        if (!$validate->scene('isCanTalk')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $params['uid'] = $this->uid;
        $res = (new RtcAnchor)->isCanTalk($params);//可以发言1
        return_ajax(1, '操作成功', $res);
    }

    /**
     * Notes:抢麦第一个用户开始发言  //设置延时队列自动取消发言状态
     * User: tangfen
     * Date: 2023/12/28
     * Time: 21:14
     */
    public function startSpeak()
    {
        $params = $this->request->post();
        // 验证
        $validate = new RtcWheatValidate;
        if (!$validate->scene('startSpeak')->check($params)) {
            return_ajax(0, $validate->getError());
        }
        $res = (new RtcWheat)->startSpeak($params);
        return_ajax(1, '操作成功', $res);
    }

}